﻿using System;
using System.Collections.Generic;
using System.IO;

namespace HeerCards.Info
{
    public class HCPlayer
    {
        public List<CostInfo> CostInfos { get; set; }
        public List<CardInfo> Cards { get; set; }
        public DateTime LastCheckinTime { get; set; }
        public int UseTimes { get; set; }//抽卡次数

        public HCPlayer()
        {
            CostInfos = new List<CostInfo>();
            Cards = new List<CardInfo>();
            
        }

        /// <summary>
        /// 增加某个货币的量
        /// </summary>
        /// <param name="name">货币名称</param>
        public void addCount(string name, int amount)
        {

            var co = CostInfos.Find((CostInfo info) => info.Name == name);
            if (co == null)
            {
                CostInfos.Add(new CostInfo() { Name = name, Count = amount });
            }
            else
            {
                co.Count += amount;
            }

        }

        public bool decreaseCount(string name, int amount)
        {
            var c = CostInfos.Find((CostInfo info) => info.Name == name);
            if (c == null)
            {
                CostInfos.Add(new CostInfo() { Name = name, Count = amount });
                return false;
            }

            if (c.Count - amount < 0)
            {
                return false;
            }
            else
            {
                c.Count -= amount;
                return true;
            }
        }
        public void addCard(HCCard card, int amount)
        {

            var t = Cards.Find((CardInfo info) => info.GUID == card.GUID);
            if (t == null)
            {
                Cards.Add(new CardInfo()
                {
                    GUID = card.GUID,
                    Count = amount,
                    Name = card.Name,
                    ExtraProbability = card.ExtraProbability,
                    Description = card.Description,
                    LocalImagePath = card.Description
                });
            }
            else
            {
                t.Count += amount;
            }
        }
        public static HCPlayer loadFromFile(string  path)
        {
                if (!Directory.Exists(path.Substring(0,path.LastIndexOf("\\"))))
                {
                    Directory.CreateDirectory(path.Substring(0, path.LastIndexOf("\\")));
                }
            if (!File.Exists(path))
            {
                File.Create(path).Close();
            }
            var str = File.ReadAllText(path).Trim();
            if (str == "")
            {
                return new HCPlayer();
            }
            else
            {
                return LitJson.JsonMapper.ToObject<HCPlayer>(str);
            }
        }
        public static void saveToFile(string path,HCPlayer player)
        {
            if (!File.Exists(path))
            {
                File.Create(path).Close();
            }
            File.WriteAllText(path, LitJson.JsonMapper.ToJson(player));
        }
    }

    public class CostInfo : CostUnit
    {
        public int Count { get; set; }

        public CostInfo()
        {
            Count = 0;
        }
    }

    public class CardInfo : HCCard
    {
        public int Count { get; set; }

        public CardInfo()
        {
            Count = 0;
        }
    }
   
}